﻿// <copyright file="Int32Collection.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    using System.ComponentModel;

    /// <summary>
    /// File name: Int32Collection.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 10/13/2008 09:59:00 h.
    /// Description: Represents a collection of Int32 values.
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>
    [TypeConverterAttribute(typeof(Int32CollectionConverter))]
    public class Int32Collection : ObservableCollection<int>
    {
        /// <summary>
        /// Parse a string to convert it to a Int32Collection.
        /// </summary>
        /// <param name="value">string that represent the Int32Collection</param>
        /// <returns>Int32Collection with the values correspoding to the string representation</returns>
        public static Int32Collection Parse(string value)
        {
            if (value.ToString().Contains(","))
            {
                Int32Collection col = new Int32Collection();
                string[] data = value.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < data.Length; i += 1)
                {
                    col.Add(Int32.Parse(data[i]));
                }
                return col;
            }
            else
            {
                Int32Collection col = new Int32Collection();
                string[] data = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < data.Length; i += 1)
                {
                    col.Add(Int32.Parse(data[i]));
                }
                return col;
            }
        }

        /// <summary>
        /// returns a string represtation of the collection
        /// </summary>
        /// <returns>string that represent the values of the collection.</returns>
        public override string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (int item in this)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                }
                sb.Append(item.ToString());
            }
            return sb.ToString();
        }
    }
}
